/******* SERVEUR TCP **************/ 
#include <stdio.h> 
#include <stdlib.h>
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <netdb.h> 
#include <string.h> 
#define PORTS "2058"
main() { int sockfd, new_fd, rv, sin_size; 

    struct addrinfo hints, *servinfo, *p;
    struct sockaddr their_addr; 
    char s[256];
    int numbytes;
    memset(&hints, 0, sizeof hints); 
    hints.ai_family = AF_UNSPEC; 
    hints.ai_socktype = SOCK_STREAM; 
    hints.ai_flags = AI_PASSIVE; // use my IP 
    rv = getaddrinfo(NULL, PORTS, &hints, &servinfo);
    if (rv != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    // Création socket et attachement
    for(p = servinfo; p != NULL; p = p->ai_next)
    {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
            perror("server: socket");
            continue;
        }

        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1)
        { 
            close(sockfd);
            perror("server: bind");
            continue;
        }
        break;
    }
    
    if (p == NULL) {
        fprintf(stderr, "server: failed to bind\n");
        return 2;
    }

    freeaddrinfo(servinfo); // Libère structure
    listen(sockfd, 5);

    while(1)
    { 
        sin_size = sizeof their_addr;
        new_fd = accept(sockfd, &their_addr, &sin_size);

        if(!fork())
        { 
            close(sockfd);
            //fgets()
            send(new_fd, "Hello!", 6, 0);
            freeaddrinfo(servinfo); // Libère structure
            
            if ((numbytes = recv(sockfd, s, 255,0) == -1))
            { 
                printf("Message : %s",s);
                exit(1);
            }
            close(new_fd); 
            exit(0); 
        } 
    }

}


